package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.CaseBaseInfo;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.Unmount;
import com.battle.pojo.entity.User;
import com.battle.pojo.enums.CaseStatusEnum;
import com.battle.pojo.enums.UnmountTypeEnum;
import com.battle.pojo.param.unmount.*;
import com.battle.pojo.vo.unmount.*;
import com.battle.pojo.dto.unmount.*;
import com.battle.service.repository.in.CaseBaseInfoRepository;
import com.battle.service.repository.in.OrganizationRepository;
import com.battle.service.repository.in.UnmountRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.UnmountService;
import com.battle.service.service.in.UserOrganizationMpService;
import nirvana.core.context.WebContext;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：战例上下架操作服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UnmountServiceImpl implements UnmountService {

    private final UnmountRepository unmountRepository;
    private final CaseBaseInfoRepository caseBaseInfoRepository;
    private final UserRepository userRepository;
    private final OrganizationRepository organizationRepository;
    private final UserOrganizationMpService userOrganizationMpService;

    public UnmountServiceImpl(UnmountRepository unmountRepository, CaseBaseInfoRepository caseBaseInfoRepository, UserRepository userRepository, OrganizationRepository organizationRepository, UserOrganizationMpService userOrganizationMpService) {
        this.unmountRepository = unmountRepository;
        this.caseBaseInfoRepository = caseBaseInfoRepository;
        this.userRepository = userRepository;
        this.organizationRepository = organizationRepository;
        this.userOrganizationMpService = userOrganizationMpService;
    }


    /**
     * 创建战例上下架操作
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createUnmount(CreateUnmountParam param) {
        Unmount unmount = new Unmount();
        BeanUtils.copyProperties(param, unmount);
        Long id = IdGenerator.SNOW.generate();
        unmount.setId(id);
        unmount.setOperationDate(LocalDateTime.now());
        unmountRepository.create(unmount);

        CaseBaseInfo caseBaseInfo = caseBaseInfoRepository.getByIdCheck(param.getCaseId());

        if (unmount.getType().getId()== UnmountTypeEnum.shelfGoods.getId()){
            caseBaseInfo.setIssueDate(LocalDateTime.now());
            caseBaseInfo.setIssueUserId(param.getOperationUserId());
            caseBaseInfo.setStatus(CaseStatusEnum.issue);
        }else{
            caseBaseInfo.setStatus(CaseStatusEnum.soldOut);
            caseBaseInfo.setSoldOutRemark(param.getRemark());
        }
        caseBaseInfoRepository.updateById(caseBaseInfo);
    }

    /**
     * 修改战例上下架操作
     *
     * @param param
     */
    @Override
    public void editUnmount(EditUnmountParam param) {
        Long id = param.getId();
        Unmount unmount = unmountRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, unmount);
        unmountRepository.edit(unmount);
    }

    /**
     * 移除战例上下架操作
     *
     * @param id
     */
    @Override
    public void removeUnmount(Long id) {
        unmountRepository.removeById(id);
    }

    /**
     * 启动/禁用战例上下架操作
     *
     * @param id
     */
    @Override
    public void enabledUnmount(Long id) {
        Unmount unmount = unmountRepository.getByIdCheck(id);
        unmount.setIsDisable(!unmount.getIsDisable());
        unmountRepository.edit(unmount);
    }

    /**
     * 获取战例上下架操作详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUnmountVo getUnmount(Long id) {
        Unmount unmount = unmountRepository.getByIdCheck(id);
        GetUnmountVo vo = new GetUnmountVo();
        BeanUtils.copyProperties(unmount, vo);
        return vo;
    }

    /**
     * 获取战例上下架操作 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUnmountListVo> getUnmountList(GetUnmountListParam param) {

        SelectUnmountDto dto = new SelectUnmountDto();
        BeanUtils.copyProperties(param, dto);
        Page<Unmount> page = unmountRepository.selectUnmount(dto);
        List<Unmount> unmountList = page.getRecords();
        List<GetUnmountListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(unmountList)){
            List<Long> userIds = new ArrayList<>();

            unmountList.forEach(v -> {
                userIds.add(v.getOperationUserId());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);

            for (Unmount unmount : unmountList) {
                GetUnmountListVo vo = new GetUnmountListVo();
                BeanUtils.copyProperties(unmount, vo);
                User operation = userMap.get(vo.getOperationUserId());
                if (operation != null) {
                    vo.setOperationUserName(operation.getName());
                    List<Organization> organizations = userOrganizationMpService.getOrganizationListByUserId(vo.getOperationUserId());
                    if (CollectionUtil.isNotEmpty(organizations)) {
                        String organizationName = organizations.stream()
                                .map(Organization::getName)
                                .collect(Collectors.joining(","));
                        vo.setOperationDepartmentName(organizationName);
                    }
                }

                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取战例上下架操作下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUnmountDownListVo> getUnmountDownList(GetUnmountDownListParam param) {
        Unmount po = new Unmount();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<Unmount> unmountList = unmountRepository.getListByEntity(po);

        List<GetUnmountDownListVo> list = new ArrayList<>();
        for (Unmount unmount : unmountList) {
            GetUnmountDownListVo vo = new GetUnmountDownListVo();
            BeanUtils.copyProperties(unmount, vo);
            list.add(vo);
        }
        return list;
    }


}



